#!/bin/sh

usage() {
    echo "usage:$0 [deploydb|deploy|xml-java-schema|premium|sdk|doc|docpremium|md|mdpremium]"
}

py() {
    if [ -d premium/test-premium ]; then
        cd premium/test-premium
    else
        cd test
    fi

    mvn test -Dtest=TestGenerateApiPythonClassAndJsonTemplate
    cd - > /dev/null
}

pysdk() {
    if [ -d premium/test-premium ]; then
        cd premium/test-premium
    else
        cd test
    fi

    mvn test -Dtest=TestGeneratePythonSDK
    cd - > /dev/null
}

apihelper() {
    cd test
    mvn test -Dtest=TestGenerateApiHelper
    cd - > /dev/null
    cp ~/ApiHelper.groovy testlib/src/main/java/org/zstack/testlib/ApiHelper.groovy

    echo "ApiHelper.groovy is generated at ~/ApiHelper.groovy"
}


doc() {
    cd test
    mvn test -Dtest=TestGenerateDocTemplate
    cd - > /dev/null
}

docpremium() {
    if [ -d premium/test-premium ]; then
        cd premium/test-premium
    else
        cd test
    fi
    mvn test -Dtest=TestGenerateDocTemplate
    cd - > /dev/null
}

md() {
    cd test
    mvn test -Dtest=TestGenerateMarkDownDoc
    cd - > /dev/null
}

mdpremium() {
    if [ -d premium/test-premium ]; then
        cd premium/test-premium
    else
        cd test
    fi

    mvn test -Dtest=TestGenerateMarkDownDoc
    cd - > /dev/null
}

i18njson() {
    ./build/zsi18n -s . -j conf/i18n.json
}

i18n() {
    ./build/zsi18n  -j conf/i18n.json -d conf/i18n/
}

foreignkey() {
    set -u
    if [ -d premium/test-premium ]; then
        cd premium/test-premium
    else
    	cd test
    fi

    mvn test -Dtest=TestGenerateSqlForeignKey
    cd - > /dev/null

    echo "see ~/zstack-sql/foreignKeys.sql"
}

notification() {
    SRC=`pwd`
    if [ -d premium/test-premium ]; then
   	cd premium/test-premium
    else
    	cd test
    fi

    mvn test -Dtest=GenerateNotificationHelper -Dsrc=$SRC
    cd - > /dev/null
}

notificationapi() {
    SRC=`pwd`
    if [ -d premium/test-premium ]; then
   	cd premium/test-premium
    else
    	cd test
    fi

    mvn test -Dtest=GenerateApiNotificationInApiMessage -Dsrc=$SRC
    cd - > /dev/null
}


triggerexpression() {
   if [ ! -d premium/mevoco ]; then
	echo "this command needs premium source code"
        exit 1
   fi

   cd premium/mevoco
   mvn -P trigger-expression generate-sources
   mkdir -p src/main/java/org/zstack/monitoring/trigger/expression/antlr4
   yes | cp target/generated-sources/antlr4/TriggerExpression* src/main/java/org/zstack/monitoring/trigger/expression/antlr4
   rm -f target/generated-sources/antlr4/TriggerExpression*
   cd - > /dev/null
}

sdk() {
    set -u
    rm -rf ~/zstack-sdk/java
    if [ -d premium/test-premium ]; then
   	cd premium/test-premium
    else
    	cd test
    fi
    
    mvn test -Dtest=TestGenerateSDK
    if [ $? -ne 0 ]; then
	echo "if you see a compile error, you may try to compile and install the sdk module first"
	exit 1
    fi
    cd - > /dev/null

    cd sdk/src/main/java/org/zstack/sdk/
    find . -type f  | grep -E -v "(ZSClient|ZSConfig|ApiException|AsyncRestState|Completion|Constants|ErrorCode|ErrorCodeList|InternalCompletion|ApiResult|Param|RestInfo|AbstractAction|QueryAction).java" | xargs rm -f
    cd -
    yes | cp ~/zstack-sdk/java/* sdk/src/main/java/org/zstack/sdk/
    set +u
}

run_profile() {
    if test x$1 = x'premium'; then
        mvn -Dmaven.test.skip=true -P premium clean install
    elif test x$1 = x'md'; then
	md
    elif test x$1 = x'sdk'; then
        sdk
    elif test x$1 = x'doc'; then
	doc
    elif test x$1 = x'docpremium'; then
        docpremium
    elif test x$1 = x'md'; then
        md
    elif test x$1 = x'mdpremium'; then
        mdpremium 
    elif test x$1 = x'py'; then
	py
    elif test x$1 = x'pysdk'; then
	pysdk
    elif test x$1 = x'apihelper'; then
	apihelper
    elif test x$1 = x'i18njson'; then
	i18njson
    elif test x$1 = x'i18n'; then
	i18n
    elif test x$1 = x'foreignkey'; then
	foreignkey
    elif test x$1 = x'notification'; then
	notification
    elif test x$1 = x'notificationapi'; then
	notificationapi
    elif test x$1 = x'triggerexpression'; then
	triggerexpression
    else
        mvn -pl build -P $1 exec:exec -D$1
    fi
}

if [ "$#" -eq 0 ]; then
    usage
    exit 1
fi

for cmd in $@
do
    run_profile ${cmd}
done
